<?php


/**
 * 通过名称，获取坐标
 * @param $address string
 * @return [lng lat] 经纬度
 */
function getCoordinateByAddress($address)
{
    $str    = urlencode($address);
    $result = file_get_contents('http://api.map.baidu.com/geocoder/v2/?address=' . $str . '&output=json&ak=zbQrD4cA5Yz7X7Pjmt3IpYcpovH2oqZs');
    $data   = json_decode($result);
    $return = [
        'lng' => (!empty($data->result->location->lng)) ? $data->result->location->lng : 0,
        'lat' => (!empty($data->result->location->lat) ? $data->result->location->lat : 0)
    ];
    return $return;
}

/*
~~~sql
$lng, $lat 经纬度

# 单位千米
// $sql   = "select *,round(ACOS(SIN(({$lat} * 3.1415) / 180 ) *SIN((lat * 3.1415) / 180 ) +COS(({$lat} * 3.1415) / 180 ) * COS((lat * 3.1415) / 180 ) *COS(({$lng} * 3.1415) / 180 - (lng * 3.1415) / 180 ) ) * 6380, 2) distance from sx_community order by distance asc limit 1";
# 单位：米
$sql   = "select *,(2 * 6378.137* ASIN(SQRT(POW(SIN(3.1415926535898*(" . $lat . "-lat)/360),2)+COS(3.1415926535898*" . $lat . "/180)* COS(lat * 3.1415926535898/180)*POW(SIN(3.1415926535898*(" . $lng . "-lng)/360),2))))*1000 as distance from `sx_community` order by distance asc limit 1";
~~~
*/